package com.wing.order.controller.admin;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.wing.common.utils.JsonResult;
import com.wing.common.utils.PageResult;
import com.wing.order.model.entity.InventoryInout;
import com.wing.order.model.entity.InventoryInoutJob;
import com.wing.order.model.request.InventoryInoutJobForm;
import com.wing.order.model.response.InventoryInoutJobVO;
import com.wing.order.service.InventoryInoutJobService;
import com.wing.order.service.InventoryInoutService;
import com.wing.warehouse.service.PalletBoxListService;
import com.wing.web.annotation.RequestObjectFieldIgnore;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 *
 * @author
 */
@Validated
@Api(value = "Admin-出/入库作业单记录管理", tags = "#/order/inventoryInoutJob")
@RestController("inventoryInoutJobController")
@RequestMapping("order/admin/inventoryInoutJob")
@RequiredArgsConstructor
public class InventoryInoutJobController {

    private final InventoryInoutJobService inventoryInoutJobService;
    private final InventoryInoutService inventoryInoutService;
    private final PalletBoxListService palletBoxListService;

    @ApiOperation(value = "查询所有出/入库作业单记录", notes = "搜索")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "第几页", defaultValue = "1", example = "1", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "limit", value = "每页多少条", defaultValue = "10", example = "10", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "sort", value="排序字段名", defaultValue = "create_time", example = "create_time", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "order", value="排序规则(asc:正序, desc:倒序)", defaultValue = "asc", example = "asc", dataType = "String", paramType = "query")
    })
    @GetMapping("/list")
    public PageResult<InventoryInoutJobVO> list(
            @RequestParam(defaultValue = "1") Integer page
            ,@RequestParam(defaultValue = "10") Integer limit
            ,@RequestParam(defaultValue = "create_time") String sort
            ,@RequestParam(defaultValue = "asc") String order
            ,InventoryInoutJob inventoryInoutJob
    ){


        //搜索
        IPage<InventoryInoutJobVO> list = inventoryInoutJobService.getInventoryInoutJobPageList(inventoryInoutJob,page,limit);
        return new PageResult<>(list.getRecords(), list.getTotal());
    }





    @ApiOperation(value = "添加出/入库作业单记录", notes = "添加")
    @RequestObjectFieldIgnore({"id", "createTime", "remark"})
    @PostMapping("/add")
    public JsonResult add(@Validated(InventoryInoutJobForm.Add.class) InventoryInoutJobForm inventoryInoutJobForm) {
        if (inventoryInoutJobService.add(inventoryInoutJobForm) > 0) {
            return JsonResult.success("添加成功");
        }
        return JsonResult.fail("添加失败");
    }




    @ApiOperation(value = "修改出/入库作业单记录", notes = "修改")
    @PutMapping("/update")
    public JsonResult update(@Validated(InventoryInoutJobForm.Update.class) InventoryInoutJobForm inventoryInoutJobForm) {
        InventoryInoutJob inventoryInoutJob = inventoryInoutJobService.getById(inventoryInoutJobForm.getId());
        if(inventoryInoutJob == null){
        return JsonResult.fail("修改失败！");
        }
        CopyOptions copyOptions = CopyOptions.create()
        .setEditable(InventoryInoutJob.class)
        .setIgnoreError(true)
        .setIgnoreNullValue(true);
        BeanUtil.copyProperties(inventoryInoutJobForm, inventoryInoutJob, copyOptions);
        if (inventoryInoutJobService.updateById(inventoryInoutJob)) {
            return JsonResult.success("修改成功！");
        }
        return JsonResult.fail("修改失败！");
    }



@ApiOperation(value = "删除出/入库作业单记录", notes = "删除")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "inventoryInoutJobId", value = "出/入库作业单记录id", required = true, dataType = "Integer", paramType = "path")
    })
    @DeleteMapping("/{id}" )
    public JsonResult delete(@PathVariable("id") Long inventoryInoutJobId) {
        if (inventoryInoutJobService.removeById(inventoryInoutJobId)) {
            return JsonResult.success("删除成功");
        }
        return JsonResult.fail("删除失败");
    }

    @ApiOperation(value = "添加出/入库作业单记录", notes = "添加")
    @PostMapping("/addInventoryInoutJob")
    public JsonResult addInventoryInoutJob(@RequestParam(value = "planId") Long planId,@RequestParam(value = "signStationId",required = false) Long signStationId,
                                           @RequestParam(value = "remark") String remark, @RequestParam(value = "quantity") Long quantity,
                                           @RequestParam(value = "productJson") String productJson,@RequestParam(value = "signStationName") String signStationName
                                           ,@RequestParam(value = "boxId",required = false) Long boxId) {

        return inventoryInoutJobService.addInventoryInoutJob(planId,signStationId,remark,quantity,productJson,signStationName,boxId);
    }
    @ApiOperation(value = "查询所有入库计划", notes = "查询")
    @GetMapping("/getStoragePlanAll")
    public JsonResult getStoragePlanAll(){
        List<InventoryInout> inventoryInoutList = inventoryInoutService.getStoragePlanAll();
        return JsonResult.success(inventoryInoutList);
    }
    @ApiOperation(value = "查询入库单", notes = "查询")
    @GetMapping("/getInventoryInoutJobById")
    public JsonResult getInventoryInoutJobById(Long id){
        InventoryInoutJobVO inoutJobVO = inventoryInoutJobService.get4appById(id);
        return JsonResult.success(inoutJobVO);
    }

    /*
    @ApiOperation(value = "查询托盘存放物资列表", notes = "搜索")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "boxId", value = "料箱id", required = true, dataType = "Long", paramType = "path")
    })

    @GetMapping("/getMaterialVOListById")
    public JsonResult<List<MaterialVO>> getMaterialVOListById(Long boxId){
        List<MaterialVO> list = orderDeliverySignService.getMaterialVOListById(boxId);
        return JsonResult.success(list);
    }*/

}
